#include<stdio.h>
#include<stdlib.h>

#define eleType int

typedef struct {
    eleType *elements;
    size_t size;
    size_t capacity;
} Stack;


//栈的创建
void StackCreate(Stack* stack){
    stack -> elements = (eleType*)malloc(sizeof(eleType)*10);
    stack -> size = 0;
    stack -> capacity = 10;
}

//栈的销毁
void StackDestroy(Stack *stk){
    free(stk -> elements);
    stk -> elements = NULL;
    stk -> size = 0;
    stk -> capacity = 0;    
}

//栈的扩容
void StackResize(Stack *stk){
    eleType *newElements = (eleType*)realloc(stk -> elements,sizeof(eleType)*stk -> capacity*2);
    stk -> elements = newElements;
    stk -> capacity *= 2;
}


//入栈
void StackPush(Stack *stk, eleType element){
    if(stk -> size == stk ->capacity){
        StackResize(stk);
    }
    stk -> elements[stk -> size] = element;

}

//出栈
eleType StackPop(Stack *stk){
    if(stk ->size == 0){
        printf("Stack underflow\n");
       exit(1);
    }
    return stk -> elements[--stk -> size];
}

//获取栈顶元素
eleType StackTop(Stack *stk){
    if( stk -> size == 0){
        printf("Stack is empty!\n");
        exit(1);

    }
    return stk -> elements[stk -> size - 1];
}